home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 585 < prev    next >
Encoding:
Text File  |  1996-08-05  |  7.0 KB  |  227 lines

  1. Path: lugb.latrobe.edu.au!lux!cs102238
  2. From: cs102238@lux.latrobe.edu.au (Gregary John Boyles )
  3. Newsgroups: comp.lang.c,alt.msdos.programmer
  4. Subject: Two strange C problems.
  5. Date: 7 Jan 1996 13:54:42 GMT
  6. Organization: La Trobe University
  7. Distribution: world
  8. Message-ID: <4cojb2$qog@lugb.latrobe.edu.au>
  9. NNTP-Posting-Host: lux.latrobe.edu.au
  10.  
  11. I have two C problems.
  12.  
  13. PROBLEM 1 :
  14.  
  15. int main(int argc,char *argv[])
  16. {
  17.      FILE *file;
  18.      char x,ch,width,height,back,fore,stringpos;
  19.      int numlines,linenum;
  20.      text_info info;
  21.      nodetypeptr listptr,posptr;
  22.  
  23.  
  24.      if (argc<2)
  25.      {
  26.           cputs("\nUsage : show file name [fore ground color] [back ground color] . . .\n");
  27.      }
  28.      else
  29.      {
  30.           file=fopen(argv[1],"rt");
  31.           if (file==NULL)
  32.           {
  33.                cprintf("\nFile %s not found . . .\n",argv[1]);
  34.           }
  35.           else
  36.           {
  37.                rewind(file);
  38.                initialize(&listptr);
  39.                linenum=1;
  40.                while (fgets(line,maxstring,file)!=NULL)
  41.                {
  42.                     removelineends(line,maxstring);
  43.                     add(&listptr,line,linenum);
  44.                     linenum++;
  45.                }
  46.                numlines=linenum--;
  47.                fclose(file);
  48.                posptr=listptr;
  49.                gettextinfo(&info);
  50.                if (argc>2)
  51.                {
  52.                     getcolors(&fore,&back,argv[2],argv[3]);
  53.                }
  54.                else
  55.                {
  56.                     fore=LIGHTGRAY;
  57.                     back=BLACK;
  58.                }
  59.                height=info.screenheight;
  60.                width=info.screenwidth;
  61.                strcpy(bottomline,"  \30  \31  \33  \32  pgup  pgdn  s (search)  help (h) esc (quit)");
  62.                for(x=strlen(bottomline)+1;x<width;x++)
  63.                {
  64.                     strcat(bottomline," ");
  65.                }
  66.                _setcursortype(_NOCURSOR);
  67.                clrscr();
  68.                textcolor(back);
  69.                textbackground(fore);
  70.                gotoxy(1,height);
  71.                cputs(bottomline);
  72.                textcolor(fore);
  73.                textbackground(back);
  74.                height--;
  75.                linenum=1;
  76.                stringpos=0;
  77.                writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
  78.                ch=space;
  79.                while (ch!=escape)
  80.                {
  81.                     while ((ch!=up) && (ch!=down) && (ch!=pageup) &&
  82.                            (ch!=pagedown) && (ch!=escape) && (ch!=left) &&
  83.                            (ch!=right) && (ch!=end_) && (ch!=home) &&
  84.                            (ch!=search) && (ch!=help))
  85.                     {
  86.                          ch=getch();
  87.                          if (ch==nul)
  88.                          {
  89.                               ch=getch();
  90.                          }
  91.                     }
  92.                     if (ch==up)
  93.                     {
  94.                          if ((linenum-1)>=1)
  95.                          {
  96.                               linenum--;
  97.                          }
  98.                          else
  99.                          {
  100.                               linenum=1;
  101.                          }
  102.                          setpos(&posptr,linenum);
  103.  
  104.                     }
  105.                     else if (ch==down)
  106.                     {
  107.                          if ((linenum+1+height)<=numlines)
  108.                          {
  109.                               linenum++;
  110.                          }
  111.                          else
  112.                          {
  113.                               linenum=numlines-height+1;
  114.                          }
  115.                          setpos(&posptr,linenum);
  116.                     }
  117.                     else if (ch==pageup)
  118.                     {
  119.                          if ((linenum-height+1)>=1)
  120.                          {
  121.                               linenum=linenum-height+1;
  122.                          }
  123.                          else
  124.                          {
  125.                               linenum=1;
  126.                          }
  127.                          setpos(&posptr,linenum);
  128.                     }
  129.                     else if (ch==pagedown)
  130.                     {
  131.                          if (((linenum+height-1)<=numlines) && ((numlines-(linenum+height-1))>=(height-1)))
  132.                          {
  133.                               linenum=linenum+height-1;
  134.                          }
  135.                          else
  136.                          {
  137.                               linenum=numlines-height+1;
  138.                          }
  139.                          setpos(&posptr,linenum);
  140.                     }
  141.                     else if (ch==left)
  142.                     {
  143.                          if (stringpos>0)
  144.                          {
  145.                               stringpos--;
  146.                          }
  147.                     }
  148.                     else if (ch==right)
  149.                     {
  150.                          if (stringpos<=(maxstring-width))
  151.                          {
  152.                               stringpos++;
  153.                          }
  154.                     }
  155.                     else if (ch==end_)
  156.                     {
  157.                          stringpos=maxstring-width+1;
  158.                     }
  159.                     else if (ch==home)
  160.                     {
  161.                          stringpos=0;
  162.                     }
  163.                     else if (ch==help)
  164.                     {
  165.                          displayhelp(fore,width,height);
  166.                     }
  167.                     else if (ch==search)
  168.                     {
  169.                          readstring(searchstring,bottomline,height,fore,back);
  170.                     }
  171.                     if (ch!=escape)
  172.                     {
  173.                          ch=space;
  174.                     }
  175.                     writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
  176.                }
  177.                while (!empty(listptr))
  178.                {
  179.                     remove(&listptr);
  180.                }
  181.                height++;
  182.                window(1,1,width,height);
  183.                clrscr();
  184.                _setcursortype(_NORMALCURSOR);
  185.           }
  186.      }
  187.      return(0);
  188. }
  189.  
  190. This program should exit such that the screen is clear with the DOS prompt
  191. in the upper left corner, but no, it exits with a short line of text on the
  192. first line and then the DOS prompt on the next line. I never (or rarely)
  193. have these sort of problems with Pascal so why do they continuously crop up
  194. with C.
  195.  
  196. PROBLEM 2 :
  197.  
  198. const escape=27;
  199. const cr=13;
  200. const bs=8;
  201.  
  202. while (ch!=cr)
  203. {
  204.      .
  205.      .
  206.      .
  207. }
  208.  
  209. while (ch!=escape)
  210. {
  211.      .
  212.      .
  213.      .
  214. }
  215.  
  216. If I replace the 27 with \27' or the 13 with '\13' then these loops don't
  217. work i.e. an infinite loop results. WHY?
  218.  
  219. Also if I want to do somthing like this : puts("\24 \25"); which should
  220. print the up and down arrows on the screen (ASCII 24 and 25), but no, it
  221. prints some other ASCII characters. As far as I can tell C has its own
  222. unique character table, at least for the control characters. WHY?
  223.  
  224. Sometimes I wonder whether some one needs to go back an rewrite the damn
  225. language so that it works sensibly and predictably like Pascal can be
  226. relied upon to do.
  227.